package com.ryan.sbnote.db;

import com.ryan.sbnote.db.DbHelper.Img;
import com.ryan.sbnote.db.DbHelper.Note;
import com.ryan.util.LogUtil;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class DbContentProvider extends ContentProvider {

	private static final String TAG = "DbContentProvider";

	private DbHelper mDbHelper;
	private SQLiteDatabase mDb;
	private UriMatcher mUriMatcher;

	private static final int CODE_NOTE = 100;
	private static final int CODE_NOTE_ID = 101;
	private static final int CODE_IMG = 200;
	private static final int CODE_IMG_ID = 201;

	@Override
	public boolean onCreate() {
		mDbHelper = new DbHelper(getContext());
		mDb = mDbHelper.getWritableDatabase();

		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		mUriMatcher.addURI("com.ryan.sbnote.provider", "note", CODE_NOTE);
		mUriMatcher.addURI("com.ryan.sbnote.provider", "note/#", CODE_NOTE_ID);
		mUriMatcher.addURI("com.ryan.sbnote.provider", "img", CODE_IMG);
		mUriMatcher.addURI("com.ryan.sbnote.provider", "img/#", CODE_IMG_ID);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		int code = mUriMatcher.match(uri);
		switch (code) {
		case CODE_NOTE:
			return mDb.query("note", projection, selection, selectionArgs,
					null, null, sortOrder);
		case CODE_NOTE_ID:
			return mDb.query("note", projection, "_id=?",
					new String[] { ContentUris.parseId(uri) + "" }, null, null,
					null);
		case CODE_IMG:
			return mDb.query("img", projection, selection, selectionArgs, null,
					null, sortOrder);
		default:
			LogUtil.w(TAG, "note match" + uri);
		}
		return null;
	}

	@Override
	public String getType(Uri uri) {
		// return "vnd.android.cursor.item.sbnote";
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		int code = mUriMatcher.match(uri);
		switch (code) {
		case CODE_NOTE:
			long id = mDb.insert("note", null, values);
			this.getContext().getContentResolver()
					.notifyChange(Note.CONTENT_URI, null);
			return ContentUris.withAppendedId(uri, id);
		case CODE_IMG:
			id = mDb.insert("img", null, values);
			this.getContext().getContentResolver()
					.notifyChange(Img.CONTENT_URI, null);
			return ContentUris.withAppendedId(uri, id);
		default:
			LogUtil.w(TAG, "note match" + uri);
			return null;
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int code = mUriMatcher.match(uri);
		switch (code) {
		case CODE_NOTE_ID:
			return mDb.delete("note", "_id=?",
					new String[] { ContentUris.parseId(uri) + "" });
		case CODE_IMG:
			return mDb.delete("note", selection, selectionArgs);
		default:
			LogUtil.w(TAG, "note match" + uri);
		}

		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int code = mUriMatcher.match(uri);
		switch (code) {
		case CODE_NOTE_ID:
			return mDb.update("note", values, "_id=?",
					new String[] { ContentUris.parseId(uri) + "" });
		case CODE_IMG:
			return mDb.update("img", values, selection, selectionArgs);
		default:
			LogUtil.w(TAG, "note match" + uri);
		}

		return 0;
	}

}
